package com.example.hasee.zhcd.Activities.XiaoXi;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.hasee.zhcd.Adapters.MessageAdapter;
import com.example.hasee.zhcd.Base.BaseActivity;
import com.example.hasee.zhcd.Bean.EventMsg;
import com.example.hasee.zhcd.R;
import com.example.hasee.zhcd.Util.GlobalParameter;
import com.example.hasee.zhcd.Util.ProgressDialogUtils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by hasee on 2018/6/5.
 */

public class MessageActivity extends BaseActivity {
    private HashMap<String, String> loginmap = (HashMap<String, String>) GlobalParameter.get("loginmap");
    private ListView message_rv;
    private TextView title;
    private ImageView back;
    private ImageView home;
    private MessageAdapter adapter;
    private ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
    private String strPhone;

    @Override
    public int contentViewId() {
        return R.layout.activity_message;
    }

    @Override
    public void set() {
        title.setText("系统消息");
        back.setOnClickListener(this);
        home.setVisibility(View.INVISIBLE);

    }

    @Override
    public void init() {
        strPhone = loginmap.get("phone");
        Log.e("strPhone", strPhone);
        message_rv = (ListView) findViewById(R.id.message_rv);
        title = (TextView) findViewById(R.id.centertitle);
        back = (ImageView) findViewById(R.id.back);
        home = (ImageView) findViewById(R.id.home);
    }

    @Override
    public void getEventData(EventMsg msg) {
        switch (msg.getAction()) {
            case "Fail":
                ProgressDialogUtils.closeProgressDialog();
                Toast.makeText(MessageActivity.this, "操作失败，请检查网络！", Toast.LENGTH_LONG).show();
                break;
            case "ZHMsg_List":
                ProgressDialogUtils.closeProgressDialog();
                try {
                    JSONObject json = new JSONObject(msg.getMsg());
                    String rspcode = json.getString("rspcode");
                    String rspmessage = json.getString("rspmessage");
                    list.clear();
                    if ("0".equals(rspcode)) {
                        JSONArray data = json.getJSONArray("data");
                        for (int i = 0; i < data.length(); i++) {
                            HashMap<String, String> mmap = new HashMap<>();
                            mmap.put("ftitle", data.getJSONObject(i).getString("ftitle"));
                            mmap.put("fdate", data.getJSONObject(i).getString("fdate"));
                            mmap.put("fid", data.getJSONObject(i).getString("fid"));
                            mmap.put("fread", data.getJSONObject(i).getString("fread"));
                            list.add(mmap);
                        }
                        adapter = new MessageAdapter(MessageActivity.this, list);
                        message_rv.setAdapter(adapter);
                        message_rv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                Intent intent = new Intent(MessageActivity.this, MessageDetailActivity.class);
                                intent.putExtra("fid", list.get(position).get("fid"));
                                startActivity(intent);
                            }
                        });

                    } else {
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
        }
    }

    private void getMsgList() {
        ProgressDialogUtils.createProgressDialog(MessageActivity.this, "正在获取系统消息列表...");
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("strPhone", strPhone);
        httpUtils.get("ZHMsg_List", map, "ZHMsg_List");
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;

        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        getMsgList();
    }
}
